home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / shutil.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  9KB  |  354 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Utility functions for copying files and directory trees.
  5.  
  6. XXX The functions here don't copy the resource fork or other metadata on Mac.
  7.  
  8. """
  9. import os
  10. import sys
  11. import stat
  12. from os.path import abspath
  13. import fnmatch
  14. __all__ = [
  15.     'copyfileobj',
  16.     'copyfile',
  17.     'copymode',
  18.     'copystat',
  19.     'copy',
  20.     'copy2',
  21.     'copytree',
  22.     'move',
  23.     'rmtree',
  24.     'Error']
  25.  
  26. class Error(EnvironmentError):
  27.     pass
  28.  
  29.  
  30. try:
  31.     WindowsError
  32. except NameError:
  33.     WindowsError = None
  34.  
  35.  
  36. def copyfileobj(fsrc, fdst, length = 16384):
  37.     '''copy data from file-like object fsrc to file-like object fdst'''
  38.     while None:
  39.         buf = fsrc.read(length)
  40.         if not buf:
  41.             break
  42.         
  43.         continue
  44.         return None
  45.  
  46.  
  47. def _samefile(src, dst):
  48.     if hasattr(os.path, 'samefile'):
  49.         
  50.         try:
  51.             return os.path.samefile(src, dst)
  52.         except OSError:
  53.             return False
  54.         
  55.  
  56.     None<EXCEPTION MATCH>OSError
  57.     return os.path.normcase(os.path.abspath(src)) == os.path.normcase(os.path.abspath(dst))
  58.  
  59.  
  60. def copyfile(src, dst):
  61.     '''Copy data from src to dst'''
  62.     if _samefile(src, dst):
  63.         raise Error, '`%s` and `%s` are the same file' % (src, dst)
  64.     _samefile(src, dst)
  65.     fsrc = None
  66.     fdst = None
  67.     
  68.     try:
  69.         fsrc = open(src, 'rb')
  70.         fdst = open(dst, 'wb')
  71.         copyfileobj(fsrc, fdst)
  72.     finally:
  73.         if fdst:
  74.             fdst.close()
  75.         
  76.         if fsrc:
  77.             fsrc.close()
  78.         
  79.  
  80.  
  81.  
  82. def copymode(src, dst):
  83.     '''Copy mode bits from src to dst'''
  84.     if hasattr(os, 'chmod'):
  85.         st = os.stat(src)
  86.         mode = stat.S_IMODE(st.st_mode)
  87.         os.chmod(dst, mode)
  88.     
  89.  
  90.  
  91. def copystat(src, dst):
  92.     '''Copy all stat info (mode bits, atime, mtime, flags) from src to dst'''
  93.     st = os.stat(src)
  94.     mode = stat.S_IMODE(st.st_mode)
  95.     if hasattr(os, 'utime'):
  96.         os.utime(dst, (st.st_atime, st.st_mtime))
  97.     
  98.     if hasattr(os, 'chmod'):
  99.         os.chmod(dst, mode)
  100.     
  101.     if hasattr(os, 'chflags') and hasattr(st, 'st_flags'):
  102.         os.chflags(dst, st.st_flags)
  103.     
  104.  
  105.  
  106. def copy(src, dst):
  107.     '''Copy data and mode bits ("cp src dst").
  108.  
  109.     The destination may be a directory.
  110.  
  111.     '''
  112.     if os.path.isdir(dst):
  113.         dst = os.path.join(dst, os.path.basename(src))
  114.     
  115.     copyfile(src, dst)
  116.     copymode(src, dst)
  117.  
  118.  
  119. def copy2(src, dst):
  120.     '''Copy data and all stat info ("cp -p src dst").
  121.  
  122.     The destination may be a directory.
  123.  
  124.     '''
  125.     if os.path.isdir(dst):
  126.         dst = os.path.join(dst, os.path.basename(src))
  127.     
  128.     copyfile(src, dst)
  129.     copystat(src, dst)
  130.  
  131.  
  132. def ignore_patterns(*patterns):
  133.     '''Function that can be used as copytree() ignore parameter.
  134.  
  135.     Patterns is a sequence of glob-style patterns
  136.     that are used to exclude files'''
  137.     
  138.     def _ignore_patterns(path, names):
  139.         ignored_names = []
  140.         for pattern in patterns:
  141.             ignored_names.extend(fnmatch.filter(names, pattern))
  142.         
  143.         return set(ignored_names)
  144.  
  145.     return _ignore_patterns
  146.  
  147.  
  148. def copytree(src, dst, symlinks = False, ignore = None):
  149.     '''Recursively copy a directory tree using copy2().
  150.  
  151.     The destination directory must not already exist.
  152.     If exception(s) occur, an Error is raised with a list of reasons.
  153.  
  154.     If the optional symlinks flag is true, symbolic links in the
  155.     source tree result in symbolic links in the destination tree; if
  156.     it is false, the contents of the files pointed to by symbolic
  157.     links are copied.
  158.  
  159.     The optional ignore argument is a callable. If given, it
  160.     is called with the `src` parameter, which is the directory
  161.     being visited by copytree(), and `names` which is the list of
  162.     `src` contents, as returned by os.listdir():
  163.  
  164.         callable(src, names) -> ignored_names
  165.  
  166.     Since copytree() is called recursively, the callable will be
  167.     called once for each directory that is copied. It returns a
  168.     list of names relative to the `src` directory that should
  169.     not be copied.
  170.  
  171.     XXX Consider this example code rather than the ultimate tool.
  172.  
  173.     '''
  174.     names = os.listdir(src)
  175.     if ignore is not None:
  176.         ignored_names = ignore(src, names)
  177.     else:
  178.         ignored_names = set()
  179.     os.makedirs(dst)
  180.     errors = []
  181.     for name in names:
  182.         if name in ignored_names:
  183.             continue
  184.         
  185.         srcname = os.path.join(src, name)
  186.         dstname = os.path.join(dst, name)
  187.         
  188.         try:
  189.             if symlinks and os.path.islink(srcname):
  190.                 linkto = os.readlink(srcname)
  191.                 os.symlink(linkto, dstname)
  192.             elif os.path.isdir(srcname):
  193.                 copytree(srcname, dstname, symlinks, ignore)
  194.             else:
  195.                 copy2(srcname, dstname)
  196.         continue
  197.         except (IOError, os.error):
  198.             why = None
  199.             errors.append((srcname, dstname, str(why)))
  200.             continue
  201.             except Error:
  202.                 err = None
  203.                 errors.extend(err.args[0])
  204.                 continue
  205.             
  206.         try:
  207.             copystat(src, dst)
  208.         except OSError:
  209.             None<EXCEPTION MATCH>(IOError, os.error)
  210.             why = None<EXCEPTION MATCH>(IOError, os.error)
  211.             if WindowsError is not None and isinstance(why, WindowsError):
  212.                 pass
  213.             else:
  214.                 errors.extend((src, dst, str(why)))
  215.         except:
  216.             isinstance(why, WindowsError)
  217.  
  218.         if errors:
  219.             raise Error, errors
  220.         errors
  221.         return None
  222.  
  223.  
  224. def rmtree(path, ignore_errors = False, onerror = None):
  225.     '''Recursively delete a directory tree.
  226.  
  227.     If ignore_errors is set, errors are ignored; otherwise, if onerror
  228.     is set, it is called to handle the error with arguments (func,
  229.     path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
  230.     path is the argument to that function that caused it to fail; and
  231.     exc_info is a tuple returned by sys.exc_info().  If ignore_errors
  232.     is false and onerror is None, an exception is raised.
  233.  
  234.     '''
  235.     if ignore_errors:
  236.         
  237.         def onerror(*args):
  238.             pass
  239.  
  240.     elif onerror is None:
  241.         
  242.         def onerror(*args):
  243.             raise 
  244.  
  245.     
  246.     
  247.     try:
  248.         if os.path.islink(path):
  249.             raise OSError('Cannot call rmtree on a symbolic link')
  250.         os.path.islink(path)
  251.     except OSError:
  252.         onerror(os.path.islink, path, sys.exc_info())
  253.         return None
  254.  
  255.     names = []
  256.     
  257.     try:
  258.         names = os.listdir(path)
  259.     except os.error:
  260.         err = None
  261.         onerror(os.listdir, path, sys.exc_info())
  262.  
  263.     for name in names:
  264.         fullname = os.path.join(path, name)
  265.         
  266.         try:
  267.             mode = os.lstat(fullname).st_mode
  268.         except os.error:
  269.             mode = 0
  270.  
  271.         if stat.S_ISDIR(mode):
  272.             rmtree(fullname, ignore_errors, onerror)
  273.             continue
  274.         
  275.         try:
  276.             os.remove(fullname)
  277.         continue
  278.         except os.error:
  279.             err = None
  280.             onerror(os.remove, fullname, sys.exc_info())
  281.             continue
  282.         
  283.  
  284.     
  285.     
  286.     try:
  287.         os.rmdir(path)
  288.     except os.error:
  289.         None<EXCEPTION MATCH>os.error
  290.         None<EXCEPTION MATCH>os.error
  291.         onerror(os.rmdir, path, sys.exc_info())
  292.     except:
  293.         None<EXCEPTION MATCH>os.error
  294.  
  295.  
  296.  
  297. def _basename(path):
  298.     return os.path.basename(path.rstrip(os.path.sep))
  299.  
  300.  
  301. def move(src, dst):
  302.     '''Recursively move a file or directory to another location. This is
  303.     similar to the Unix "mv" command.
  304.  
  305.     If the destination is a directory or a symlink to a directory, the source
  306.     is moved inside the directory. The destination path must not already
  307.     exist.
  308.  
  309.     If the destination already exists but is not a directory, it may be
  310.     overwritten depending on os.rename() semantics.
  311.  
  312.     If the destination is on our current filesystem, then rename() is used.
  313.     Otherwise, src is copied to the destination and then removed.
  314.     A lot more could be done here...  A look at a mv.c shows a lot of
  315.     the issues this implementation glosses over.
  316.  
  317.     '''
  318.     real_dst = dst
  319.     if os.path.isdir(dst):
  320.         real_dst = os.path.join(dst, _basename(src))
  321.         if os.path.exists(real_dst):
  322.             raise Error, "Destination path '%s' already exists" % real_dst
  323.         os.path.exists(real_dst)
  324.     
  325.     
  326.     try:
  327.         os.rename(src, real_dst)
  328.     except OSError:
  329.         if os.path.isdir(src):
  330.             if destinsrc(src, dst):
  331.                 raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
  332.             destinsrc(src, dst)
  333.             copytree(src, real_dst, symlinks = True)
  334.             rmtree(src)
  335.         else:
  336.             copy2(src, real_dst)
  337.             os.unlink(src)
  338.     except:
  339.         os.path.isdir(src)
  340.  
  341.  
  342.  
  343. def destinsrc(src, dst):
  344.     src = abspath(src)
  345.     dst = abspath(dst)
  346.     if not src.endswith(os.path.sep):
  347.         src += os.path.sep
  348.     
  349.     if not dst.endswith(os.path.sep):
  350.         dst += os.path.sep
  351.     
  352.     return dst.startswith(src)
  353.  
  354.